package com.unytech.project.web.listener;

import java.util.List;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.unytech.project.common.Globals;
import com.unytech.project.entity.system.Permission;
import com.unytech.project.utils.PermissionUtil;

public class InitSystemContextListener implements ServletContextListener, ServletContextAttributeListener {
	private final Log logger = LogFactory.getLog(this.getClass());
	
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		logger.info("---监听到初始化ServletContext---");
		logger.info("写入全局变量：容器上下文路径");
		sce.getServletContext().setAttribute(Globals.CONTEXT_ALLPERMISSIONS_KEY, sce.getServletContext().getContextPath());
		
		logger.info("写入全局变量：全部系统权限");
		List<Permission> permissionlist = null;
		try {
			permissionlist = PermissionUtil.getXMLPermission();
		} catch (Exception e) {
			logger.info(e.getMessage());
			logger.error(e, e);
		}
		sce.getServletContext().setAttribute(Globals.CONTEXT_ALLPERMISSIONS_KEY, permissionlist);
		
	}
	
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		logger.info("---监听到销毁ServletContext---");	
	}

	@Override
	public void attributeAdded(ServletContextAttributeEvent arg0) {
		logger.info("---监听到ServletContextAttribute的添加---");
	}

	@Override
	public void attributeRemoved(ServletContextAttributeEvent arg0) {
		logger.info("---监听到ServletContextAttribute的移除---");
	}

	@Override
	public void attributeReplaced(ServletContextAttributeEvent arg0) {
		logger.info("---监听到ServletContextAttribute的替换---");
	}
	
}
